/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bf;


import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.*;

/*
 * Extremadamente primitivo, una version mejor incluiria
 * un pool manager.
 */

/**
 *
 * @author Juan Lucas
 */
public class DataAccess
{
    
    
    public static void dmlQueryStatic(Connection conn, String query) throws SQLException
    {
         Statement s = conn.createStatement ();
         s.executeUpdate (query);
    }
    
    public static ResultSet selectQueryStatic(Connection conn, String query) throws SQLException
    {
         Statement s = conn.createStatement ();
         return s.executeQuery (query);
    }
    
    public static boolean existenceQueryStatic(Connection conn, String query) throws SQLException
    {
         Statement s = conn.createStatement ();
         s.executeQuery (query);
         ResultSet rs = s.getResultSet ();
         if (rs.next())
         {
             return true;
         }
         else
         {
             return false;
         }        
    }
    
    public static int insertionQueryStatic(Connection conn, String query)
    throws SQLException
    {
    	Statement stmt = conn.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY,
                 java.sql.ResultSet.CONCUR_UPDATABLE);
  
    	stmt.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);

    	    //
    	    // Example of using Statement.getGeneratedKeys()
    	    // to retrieve the value of an auto-increment
    	    // value
    	    //
    	    int autoIncKeyFromApi = -1;
    	    ResultSet rs = stmt.getGeneratedKeys();
    	    rs = stmt.getGeneratedKeys();

    	    if (rs.next()) {
    	        autoIncKeyFromApi = rs.getInt(1);
    	    } else {
    	        throw new SQLException();
    	    }
    	    rs.close();
    	    return autoIncKeyFromApi;
    }
    
    
    /**
     * Cerramos eventuales conexiones abiertas.
     * @throws Throwable 
     */
 
    
}
